package com.qyhl.webtv.module_news.news.smallvideo.fragment;


import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.module_news.common.NewsUrl;
import com.qyhl.webtv.module_news.utils.NewsConstant;

import java.util.List;

/**
 * Created by qyhl on 2017/12/6.
 */

public class SmallVideoFragmentModel implements SmallVideoFragmentContract.SVFragmentModel {
    private SmallVideoFragmentPresenter mPresenter;

    public SmallVideoFragmentModel(SmallVideoFragmentPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取小视频列表
     *
     * @param sectionId
     * @param page
     */
    @Override
    public void getDatas(String sectionId, final String page) {
        //最新推荐
        if ("0".equals(sectionId)) {
            EasyHttp.get(NewsUrl.GET_RECOMMEND_VIDEO)
                    .params("siteId", NewsConstant.OWNER_SITE_ID + "")
                    .params("sectionId", "1000010,1000009,1000008,1000007,1000006")
                    .params("page", page)
                    .execute(new SimpleCallBack<List<NewsBean>>() {
                        @Override
                        public void onError(ApiException e) {
                            if (e.getCode() == 202) {
                                if ("1".equals(page)) {
                                    mPresenter.setError(2, "暂无任何内容！");
                                } else {
                                    mPresenter.setError(3, "no more");
                                }
                            } else {
                                if ("1".equals(page)) {
                                    mPresenter.setError(4, "解析出错，加载失败！");
                                } else {
                                    mPresenter.setError(5, "解析出错，加载失败！");
                                }
                            }
                        }

                        @Override
                        public void onSuccess(List<NewsBean> newsBeans) {
                            if (newsBeans != null && newsBeans.size() > 0) {
                                if ("1".equals(page)) {
                                    mPresenter.setDatas(newsBeans, false);
                                } else {
                                    mPresenter.setDatas(newsBeans, true);
                                }
                            } else {
                                if ("1".equals(page)) {
                                    mPresenter.setError(2, "暂无任何内容！");
                                } else {
                                    mPresenter.setError(3, "no more");
                                }
                            }
                        }
                    });
            //其他栏目
        } else {
            EasyHttp.get(NewsUrl.GET_SMALL_VIDEO_OTHER)
                    .params("siteId", NewsConstant.OWNER_SITE_ID + "")
                    .params("sectionId", sectionId)
                    .params("page", page)
                    .params("number", "10")
                    .execute(new SimpleCallBack<List<NewsBean>>() {
                        @Override
                        public void onError(ApiException e) {
                            if (e.getCode() == 202) {
                                if ("1".equals(page)) {
                                    mPresenter.setError(2, "暂无任何内容！");
                                } else {
                                    mPresenter.setError(3, "no more");
                                }
                            } else {
                                if ("1".equals(page)) {
                                    mPresenter.setError(4, "解析出错，加载失败！");
                                } else {
                                    mPresenter.setError(5, "error");
                                }
                            }
                        }

                        @Override
                        public void onSuccess(List<NewsBean> newsBeans) {
                            if (newsBeans != null && newsBeans.size() > 0) {
                                if ("1".equals(page)) {
                                    mPresenter.setDatas(newsBeans, false);
                                } else {
                                    mPresenter.setDatas(newsBeans, true);
                                }
                            } else {
                                if ("1".equals(page)) {
                                    mPresenter.setError(2, "暂无任何内容！");
                                } else {
                                    mPresenter.setError(3, "no more");
                                }
                            }
                        }
                    });

        }
    }
}
